import { Common } from '../../utils/common.js';
var common = new Common();

var address = require('../../utils/city.js')
var animation

Page({
  /**
   * 页面的初始数据
   */
  data: {
    year: new Date().getFullYear(), // 年份
    month: new Date().getMonth() + 1, // 月份
    day: new Date().getDate(), // 日期
    show_calendar: false,
    cs: 30, // 单元格大小
    title_type: 'cn', // 周标题类型
    titleType: ['中文简写'],
    title_index: 0,
    style: [],
    activeType: 'square', // 日期背景效果
    days_addon: [],

    header: true, // 日历标题
    lunar: false, // 显示农历
    more: false, // 显示非当前月日期                
    week_title: true, // 显示周标题
    next: true, // 显示下个月
    prev: true, // 显示上个月
    menuType: 0,
    begin: null,
    status: 1,
    end: null,
    isVisible: false,
    animationData: {},
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    countrys: [],
    provinces: [],
    citys: [],
    country: '',
    province: '',
    city: '',
    ticket_info: '',
    navigatenumber:'1',
  },
  onClose() {
    this.setData({ show_calendar: false });
  },

  go_ticket_date: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    that.setData({
      id: id,
      show_calendar: true
    })
  },
  dayClick: function (event) {
    const year = event.detail.year;
    const month = event.detail.month;
    const day = event.detail.day;
    const color = event.detail.color;
    const lunarMonth = event.detail.lunarMonth;
    const lunarDay = event.detail.lunarDay;
    const background = event.detail.background;
    const go_time = year + "-" + month + "-" + day
    this.setData({
      style: [{ year: year, month: month, day: day, color: 'white', background: '#BFB197' }],
      go_time: go_time
    })
  },
  confirm_time: function () {
    var time = this.data.go_time;
    var id = this.data.id;
    if (id == 1) {
      this.setData({
        show_calendar: false,
        date1: time
      })
    } else {
      this.setData({
        show_calendar: false,
        date2: time
      })
    }

    //1 代表出行  2  代表返回
    console.log(this.data.id);
    console.log(this.data.time);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取机票
    this.TicketInfo(options);
    // 初始化动画变量
    var animation = wx.createAnimation({
      duration: 500,
      transformOrigin: "50% 50%",
      timingFunction: 'ease',
    })
    this.animation = animation;
    // 默认联动显示北京
    var id = address.countrys[0].id
    this.setData({
      countrys: address.countrys,
      provinces: address.provinces[id],
      citys: address.citys[address.provinces[id][0].id],
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var number = this.data.navigatenumber;
    if (number > 1)
      wx.navigateBack({
        delta: number
      })
  },

  //获取机票信息
  TicketInfo: function (options) {
    var that = this;
    var datas = {
      url: 'ticket/info',
      type:'post'
    }
    common.ToData(datas,data => {
      // if(data!="")
      //   var navigatenumber = '1';
      // if (options.navigatenumber)
      //   var navigatenumber = options.navigatenumber;
      that.setData({
        ticket_info: data,
        // navigatenumber: navigatenumber
      })
    })
  },

  //保存机票
  formSubmit: function (e) {
    var f = e.detail.value;
    var that = this;
    var datas = {
      data:f,
      type:'post',
      url:'ticket'
    }
    common.DataToCallBack(datas, data => {
      if (data.code == 201) {
        //提示成功 跳转
        wx.showToast({
          title: '成功',
          icon: 'success',// 'none'为无图标
          duration: 2000,//多少毫秒后消失      
        });
        that.setData({
          navigatenumber: '2',
          back:true
        })
      } else {
        var e_title = "机票信息填写有误";
        var key = '';
        for (var key in data.msg) {
          key = key;
          break;
        }
        if (key)
          e_title = data.msg[key];
        //提示失败
        wx.showToast({
          title: e_title,
          icon: 'none',// 'none'为无图标
          duration: 2000 //多少毫秒后消失
        });
      }
    })
  },

  //日期控件
  bindDateChange: function (e) {
    var date = 'date' + e.currentTarget.dataset.index;

    this.setData({
      [date]: e.detail.value
    })
  },

  //时间控件
  bindTimeChange: function (e) {
    var time = 'time' + e.currentTarget.dataset.index;
    this.setData({
      [time]: e.detail.value
    })
  },

  hideMenuTap: function (e) {
    this.startAnimation(false, -200)
  },

  // 执行动画
  startAnimation: function (isShow, offset) {
    var that = this
    var offsetTem
    if (offset == 0) {
      offsetTem = offset
    } else {
      offsetTem = offset + 'rpx'
    }
    this.animation.translateY(offset).step()
    this.setData({
      animationData: this.animation.export(),
      isVisible: isShow
    })
    console.log(that.data)
  },

  sureDateTap: function () {
    this.data.pageNo = 1
    this.startAnimation(false, -200)
  },

  // 点击所在地区弹出选择框
  selectDistrict: function (e) {
    var that = this
    that.setData({
      select_type: e.currentTarget.dataset.index
    })
    if (e.currentTarget.dataset.info) {
      this.setData({
        select_type: e.currentTarget.dataset.index
      })
    }

    if (that.data.addressMenuIsShow) {
      return
    }
    that.startAddressAnimation(true)
  },

  // 执行动画
  startAddressAnimation: function (isShow) {
    var that = this
    if (isShow) {
      that.animation.translateY(0 + 'vh').step()
    } else {
      that.animation.translateY(40 + 'vh').step()
    }
    that.setData({
      animationAddressMenu: that.animation.export(),
      addressMenuIsShow: isShow,
    })
  },

  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.startAddressAnimation(false)
  },

  // 点击地区选择确定按钮
  citySure: function (e) {
    var that = this
    var city = that.data.city
    var value = that.data.value
    that.startAddressAnimation(false)
    // 将选择的城市信息显示到输入框
    var areaInfo_key = 'areaInfo' + that.data.select_type;
    if (that.data.select_areaInfo)
      var areaInfo_value = that.data.select_areaInfo;
    else
      var areaInfo_value = that.data.countrys[value[0]].name + ',' + that.data.provinces[value[1]].name + ',' + that.data.citys[value[2]].name;

    console.log(areaInfo_value)
    that.setData({
      [areaInfo_key]: areaInfo_value,
    })
  },

  hideCitySelected: function (e) {
    this.startAddressAnimation(false)
  },

  // 处理省市县联动逻辑
  cityChange: function (e) {
    var value = e.detail.value
    var countrys = this.data.countrys
    var provinces = this.data.provinces
    var citys = this.data.citys
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    if (this.data.value[0] != provinceNum) {
      var id = countrys[provinceNum].id
      this.setData({
        value: [provinceNum, 0, 0],
        provinces: address.provinces[id],
        citys: address.citys[address.provinces[id][0].id],
      })
    } else if (this.data.value[1] != cityNum) {
      var id = provinces[cityNum].id
      this.setData({
        value: [provinceNum, cityNum, 0],
        citys: address.citys[provinces[cityNum].id],
      })
    } else {
      this.setData({
        value: [provinceNum, cityNum, countyNum]
      })
    }
    console.log(this.data)
  },
})